package org.aceor.funny.common.http.utils;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by lxue on 15/11/21.
 */
public class Utils {

    private static final Logger log = LoggerFactory.getLogger(Utils.charset);
    public static final String charset = "UTF-8";
    public static List<NameValuePair> convertMap2Pair(Map<String,Object> map){
        int size = map.size();
        List<NameValuePair> list;
        if(0 == size){
            list = Collections.emptyList();
        }else{
            list = new ArrayList<>(size);
            for(Map.Entry<String, Object> entry:map.entrySet()){
                list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        return list;
    }


    public static String urlParamEncode(String value){
        try {
            return URLEncoder.encode(value, charset);
        } catch (UnsupportedEncodingException e) {
            log.error("url编码失败",e);
            return value;
        }
    }
}
